﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Hr
{
    public partial class frmManageDepartment : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstDepartmentEntity mSelectedDepartment;
        #endregion

        #region Constructor
        public frmManageDepartment()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComobox()
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbDeleteFlag, typeof(DeleteFlag), typeof(string), true);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(colSTATUS, typeof(DeleteFlag), typeof(int), true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtParentDepartment.Tag != null && txtParentDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity parent = (MstDepartmentEntity)txtParentDepartment.Tag;
                condition += " AND D.PARENT_ID = '" + parent.DepartmentId + "' ";
            }
            if (txtManager.Tag != null && txtManager.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity manager = (MstPersonEntity)txtManager.Tag;
                condition += " AND D.MANAGER = '" + manager.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(txtDepartmentCode.Text) == false)
            {
                condition += " AND D.DEPARTMENT_CODE LIKE '%" + txtDepartmentCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtDepartmentName.Text) == false)
            {
                condition += " AND D.DEPARTMENT_NAME LIKE '%" + txtDepartmentName.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbDeleteFlag.Text) == false && cmbDeleteFlag.SelectedValue != null)
            {
                condition += " AND D.DELETE_FLAG = " + cmbDeleteFlag.SelectedValue.ToString() + "";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "SEQUENCE";
            DataTable dt = MstDepartmentBs.GetDapartmentByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        private void frmManageDepartment_Load(object sender, EventArgs e)
        {
            BindComobox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedDepartment = new MstDepartmentEntity(dr);
        }

        protected override void Click_View()
        {
            if (mSelectedDepartment == null)
            {
                MessageBox.Show("请选择一个部门！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintianDepartment frm = new frmMaintianDepartment();
            frm.ThisActionType = ActionType.View;
            frm.SelectedDepartment = mSelectedDepartment;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedDepartment == null)
            {
                MessageBox.Show("请选择一个部门！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中部门？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedDepartment.DeleteFlag = 1;
                bool successful = MstDepartmentBs.UpdateMstDepartment(mSelectedDepartment);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedDepartment == null)
            {
                MessageBox.Show("请选择一个部门！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintianDepartment frm = new frmMaintianDepartment();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedDepartment = mSelectedDepartment;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintianDepartment frm = new frmMaintianDepartment();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtParentDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtParentDepartment.Text = frm.SelectedDepartment.DepartmentName;
                txtParentDepartment.Tag = frm.SelectedDepartment;
            }
            else
            {
                txtParentDepartment.Text = "";
                txtParentDepartment.Tag = null;
            }
        }

        private void txtManager_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtManager.Text = frm.SelectedPerson.NameCn;
                txtManager.Tag = frm.SelectedPerson;
            }
            else
            {
                txtManager.Text = "";
                txtManager.Tag = null;
            }
        }
        #endregion



    }
}
